﻿using System;
using System.Collections.Generic;
using System.Dynamic;
using System.Linq;
using System.Web.Mvc;
using Xenta.Attributes;
using Xenta.Utils;
using Xenta.Web.Controllers;

namespace Xenta.Web.Areas.Forum.Controllers
{
    public sealed class TopicController : ExtController
    {
        #region GET/POST Actions

        [HttpGet, RestoreState]
        public ActionResult Show(int id, 
            string searchTerm, 
            int? page)
        {
            dynamic m = new ExpandoObject();

            m.Topic = Svc.ProcessOperation("GetForumTopic", new
            {
                EntityID = id
            }, new
            {
                DisclosureLevel = "Entire"
            });
            if(m.Topic == null)
                return HttpNotFound();

            m.Forum = Svc.ProcessOperation("GetForum", new
            {
                EntityID = JDM.Int32(m.Topic.Forum.EntityID)
            }, new
            {
                DisclosureLevel = "Entire",
                DisclosureOptions = new Dictionary<String, String>
                {
                    { "TranslatedTo", JDM.String(Svc.Localization.Language.Code) }
                }.Unload()
            });
            m.UserCanModerate = false;
            m.UserCanWrite = false;
            if(Svc.IsAuthenticated)
            {
                m.UserCanWrite = JDM.HasFlags(m.Topic.Flags, "Active");
                var acl = m.Forum.AccessRules as IEnumerable<dynamic>;
                if(acl.Any())
                {
                    var tmp = acl.Where(x => Svc.InRole((string)JDM.String(x.Role.Code)))
                        .Select(x => JDM.String(x.AccessRights));
                    m.UserCanModerate = tmp.Any(x => x.Contains("Moderate"));
                    m.UserCanWrite = m.UserCanModerate || (m.UserCanWrite && 
                        tmp.Any(x => x.Contains("ReadWrite")));
                }
            }

            page = page.HasValue ? page.Value : 0;
            m.CountPerPage = Svc.Config(Par.In("WebPortal").Of(this).Group("Show").Name("Count"), 10);
            m.Posts = Svc.ProcessOperation("SearchForumPosts", new
            {
                Term = new
                {
                    Expression = String.Format("%{0}%", searchTerm)
                },
                TopicID = id,
                Index = page * m.CountPerPage,
                Count = m.CountPerPage,
                SortBy = new[] 
                {
                    "CreatedOn"
                },
                SortDir = "Ascending"
            }, new
            {
                DisclosureLevel = "Extended"
            });
            return View(m);
        }

        #endregion

        #region Child Actions

        [ChildActionOnly]
        public ActionResult _Popular(int? days)
        {
            var m = Svc.ProcessOperation("SearchForumTopics", new
            {
                Count = Svc.Config(Par.In("WebPortal").Of(this).Group("_Popular").Name("Count"), 10),
                CreatedFrom = days.HasValue ? DateTime.UtcNow.Date.AddDays(-days.Value) : (DateTime?)null,
                SortBy = new[] 
                {
                    "PostCount"
                },
                SortDir = "Descending"
            }, new
            {
                DisclosureLevel = "Base"
            });
            return PartialView(m);
        }

        [ChildActionOnly]
        public ActionResult _Latest()
        {
            var m = Svc.ProcessOperation("SearchForumTopics", new
            {
                Count = Svc.Config(Par.In("WebPortal").Of(this).Group("_Latest").Name("Count"), 10),
                SortBy = new[] 
                {
                    "CreatedOn"
                },
                SortDir = "Descending"
            }, new
            {
                DisclosureLevel = "Base"
            });
            return PartialView(m);
        }

        #endregion
    }
}
